package edu.ptu.communityplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.ptu.communityplatform.common.controller.AjaxResponse;
import edu.ptu.communityplatform.common.controller.BaseController;
import edu.ptu.communityplatform.common.controller.TableInfo;
import edu.ptu.communityplatform.domain.entity.Plot;
import edu.ptu.communityplatform.domain.queryParam.plotParam.PlotHandleParam;
import edu.ptu.communityplatform.domain.queryParam.plotParam.PlotUpdateParam;
import edu.ptu.communityplatform.domain.vos.PlotVo;
import edu.ptu.communityplatform.service.ICarService;
import edu.ptu.communityplatform.service.IParkingLotService;
import edu.ptu.communityplatform.service.IPersonService;
import edu.ptu.communityplatform.service.IPlotService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import static edu.ptu.communityplatform.mapping.PlotMapping.PLOT_MAPPING;

/**
 * @BelongsProject: communityPlatform
 * @BelongsPackage: edu.ptu.communityplatform.controller
 * @Author: HYJ
 * @CreateTime: 2025-04-08  23:34
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/plot")
public class PlotController extends BaseController {
    @Resource
    private IPlotService plotService;

    @Resource
    private ICarService carService;

    @Resource
    private IParkingLotService parkingLotService;

    @Resource
    private IPersonService personService;

    /**
     * 功能描述: 根据id查询小区信息
     *
     * @param plotId 小区id
     * @return {@link AjaxResponse}<{@link PlotVo}>
     */
    @GetMapping("/dtl/{plotId}")
    public AjaxResponse getDtlById(@PathVariable Long plotId) {


        Plot plot = plotService.getById(plotId);

        // 转换为 PlotVo
        PlotVo plotVo = PLOT_MAPPING.plotToPlotVo(plot);

        return plotVo != null ? AjaxResponse.success(plotVo) : AjaxResponse.fail("查询失败");
    }

    /**
     * 功能描述: 根据id删除小区信息
     *
     * @param plotId   小区id
     * @param pageNum
     * @param pageSize
     * @param plotName
     * @param plotNo
     * @return {@link AjaxResponse}
     */
    @GetMapping("/list")
    public TableInfo list(
            @RequestParam(required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(required = true, defaultValue = "5") Integer pageSize,
            @RequestParam(required = false) String plotName,
            @RequestParam(required = false) String plotNo
    ) {
        IPage<Plot> page = plotService.getPage(pageNum, pageSize, plotName, plotNo);

        // 转换为 PlotVo
        List<PlotVo> plotVos = page.convert(PLOT_MAPPING::plotToPlotVo).getRecords();
        return toJson(page != null, plotVos, plotVos.size());

    }

    /**
     * 功能描述: 添加小区信息
     *
     * @param param 小区信息
     * @return {@link AjaxResponse}
     */
    @PostMapping()
    public AjaxResponse add(@RequestBody PlotHandleParam param) {

        String plotName = param.getPlotName();
        String city = param.getCity();
        String cityDtl = param.getCityDtl();
        String coordinate = param.getCoordinate();

        String plotNo = UUID.randomUUID().toString().replace("-", "");

        // 校验参数
        if (plotName == null || Objects.equals("", plotName)) {
            return AjaxResponse.fail("小区名称不能为空");
        }

        if (city == null || Objects.equals("", city)) {
            return AjaxResponse.fail("小区所在城市不能为空");
        }

        if (cityDtl == null || Objects.equals("", cityDtl)) {
            return AjaxResponse.fail("小区所在城市详情不能为空");
        }
        if (coordinate == null || Objects.equals("", coordinate)) {
            return AjaxResponse.fail("城市不能为空");
        }

        // 构建 Plot 对象
        Plot plot = new Plot();
        plot.setPlotName(plotName);
        plot.setCity(city);
        plot.setCityDtl(cityDtl);
        plot.setCoordinate(coordinate);
        plot.setPlotNo(plotNo);


        // 调用服务层方法添加小区信息
        boolean save = plotService.save(plot);
        return save ? AjaxResponse.success("success") : AjaxResponse.fail("fail");

    }

    /**
     * 功能描述: 修改小区信息
     *
     * @param param 小区信息
     * @return {@link AjaxResponse}
     */
    @PutMapping()
    public AjaxResponse update(@RequestBody PlotUpdateParam param) {

        String city = param.getCity();
        String cityDtl = param.getCityDtl();
        String coordinate = param.getCoordinate();
        Long plotId = param.getPlotId();
        String plotName = param.getPlotName();
        // 校验参数
        if (plotName == null || Objects.equals("", plotName)) {
            return AjaxResponse.fail("小区名称不能为空");
        }

        if (city == null || Objects.equals("", city)) {
            return AjaxResponse.fail("小区所在城市不能为空");
        }

        if (cityDtl == null || Objects.equals("", cityDtl)) {
            return AjaxResponse.fail("小区所在城市详情不能为空");
        }

        if (coordinate == null || Objects.equals("", coordinate)) {
            return AjaxResponse.fail("城市不能为空");
        }

        LambdaQueryWrapper<Plot> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Plot::getPlotId, plotId);

        //更新参数
        Plot plot = new Plot();
        plot.setCity(city);
        plot.setPlotName(plotName);
        plot.setCoordinate(plotName);
        plot.setCityDtl(cityDtl);

        boolean update = plotService.update(plot, wrapper);

        return update ? AjaxResponse.success("succuess") : AjaxResponse.fail("error");

    }

    /**
     * 功能描述: 删除小区信息
     *
     * @param plotId 小区id
     * @return {@link AjaxResponse}
     */
    @DeleteMapping("/{plotId}")
    public AjaxResponse delete(@PathVariable Long plotId) {

        // 调用服务层方法删除小区信息
        boolean removed = plotService.removeById(plotId);
        return removed ? AjaxResponse.success("success") : AjaxResponse.fail("error");
    }
}
